package com.haiyou.common.spring.schedule;

import java.io.Serializable;
import java.util.concurrent.Delayed;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.springframework.util.Assert;

import com.haiyou.common.thread.task.AbstractRunnableCoreTask;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 
 * 
 * @Description: 无回调定时任务
 * @author xingyuan
 * @date 2022年7月6日 下午3:32:28
 */
@Getter
public abstract class RunnableScheduledTask extends AbstractRunnableCoreTask 
					implements Delayed,Serializable{

	private static final long serialVersionUID = 3181827832358953079L;

	/**
	 * Futrue
	 */
	@Setter
	private ScheduledFuture<?> futrue;
	
	/**
	 * 校验任务是否提交到定时器
	 */
	private void checksubmit() {
		Assert.notNull(futrue,"task not submit in secheduled");
	}
	
	/**
	 * @param mayInterruptIfRunning 是否运行时中断,false不允许在线程运行时中断
	 */
	public boolean cancel(boolean mayInterruptIfRunning) {
		checksubmit();
		return futrue.cancel(mayInterruptIfRunning);
	}
	
	public boolean isCancelled() {
		checksubmit();
		return futrue.isCancelled();
	}
	
	public boolean isDone() {
		checksubmit();
		return futrue.isDone();
	}

	/**
	 *  等待 ScheduledFuture 执行完成
	 */
	public void futrueGet()  throws InterruptedException, ExecutionException {
		checksubmit();
		futrue.get();
	}
	
	
	/**
	 * 等待 ScheduledFuture 执行完成
	 * @param timeout
	 * @param unit
	 */
	public void futrueGet(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
		checksubmit();
		futrue.get(timeout,unit);
	}
	

	@Override
	public long getDelay(TimeUnit unit) {
		checksubmit();
		return futrue.getDelay(unit);
	}

	@Override
	public int compareTo(Delayed o) {
		checksubmit();
		return futrue.compareTo(o);
	}

	
}
